//规则, 组件名字驼峰, 元素标签中划线分割, 两者映射缺一不可.
angular.module('app').component('mTree', {
    templateUrl: getTemplateUrl('matrix/component/tpl/mTree.html'),
    bindings: {
        blur: '&',
        treeData: "<",
        treeBlur: '&',
        treeType: '@',
        selection: '@',
        datatype:"<",
    },
    controller: function ($scope, $element, $http, $attrs, $modal, $rootScope) {
        var ctrl = this;

        var apple_selected, tree, treedata_avm, treedata_geography;
        ctrl.my_tree_handler = function (branch) {
            ctrl.treeBlur({
                info: branch
            })
        };
        var datatype = ctrl.datatype;
        $.ajax({
           type: "get",
           url: matrix_product_url+datatype,
           async:false,
           success:function(data){
               treedata_avm =data.data;
           }
       });

        ctrl.my_data = treedata_avm;
        ctrl.my_tree = tree = {};
        ctrl.isTrue = {};
        ctrl.isTrue.lable = "只显示需求改动的原型";
        ctrl.isTrue.check = false;
        ctrl.selectedBranch = {};
        ctrl.try_adding_a_branch = function () {
            ctrl.selectedBranch = tree.get_selected_branch();
            console.log("ctrl.selectedBranch",ctrl.selectedBranch);

            $rootScope.popWindow(ctrl.selectedBranch,function (data) {
                console.log("ctrl.selectedBranch",ctrl.selectedBranch);
                console.log("data",data.label);
                ctrl.blur({
                    info: data
                })
                /*tree.add_branch(ctrl.selectedBranch, {
                    label: data.lable
                });*/
/*
                data.label = b.label;
*/
            })
            /*$.ajax({
                type: "post",
                url: matrix_product_url+"/menu/save",
                data: JSON.stringify(ctrl.selectedBranch),
                contentType: "application/json; charset=utf-8",
                async:false,
                success:function(data){
                    console.log(data.desc);
                },
                error:function(data){
                    console.log(data.desc);
                }
            });*/
        };

        ctrl.updateBranch = function () {
            var b;
            b = tree.get_selected_branch();
/*
            console.log('b', b);
*/
            $rootScope.popWindow(b, function (data) {
                ctrl.blur({
                    info: data
                })
                /*b.label = data.label;
                console.log(data);*/
/*
                b.label = data.label;
*/
                /*$.ajax({
                    type: "post",
                    url: matrix_product_url+"/menu/save",
                    data: JSON.stringify(b),
                    contentType: "application/json; charset=utf-8",
                    async:false,
                    success:function(data){
                        console.log(data.desc);
                    },
                    error:function(data){
                        console.log(data.desc);
                    }
                });*/
                /*tree.(b, {
                    label: data.lable
                });*/
            })
            /*$.ajax({
                type:"get",
                url:matrix_product_url+"/tree/",
                async:false,
                success:function(data){

                }
            })*/
        }

        ctrl.delBranch = function(){
            var b;
            b = tree.get_selected_branch();
                var parent_b = tree.get_parent_branch(b);
                console.log(parent_b);
                angular.forEach(parent_b,function(obj){
                    console.log()
                })


        }
        ctrl.set_true_branch = function (istrue) {
            console.log(istrue)

            // var trees=[];
            // if(istrue.check){
            // 	angular.forEach(ctrl.my_data, function(tree){
            // 		console.log(tree)
            // 		if(tree.status!=undefined){
            // 			trees.push(tree)
            // 		}
            // 	})
            // }
            // return  ctrl.my_data=trees;
        }

    }
});
